// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','LocalStorageModule'])

.constant("SERVER_BASE_URL", "http://192.168.191.1:8080/fresh365/")

.run(function($ionicPlatform,Session,localStorageService,AuthService,Cart) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
    }
    
  });
  	//用户自动登录
  	var loginInfo =localStorageService.get("loginInfo");
	var isUndefined=angular.isUndefined(loginInfo);//localStorage中currentUser是否已定义
	if(!isUndefined && loginInfo!=null){	 
		AuthService.login(loginInfo).then(function(result){
			if(result.result==1){
			 	Session.create(result.obj);
			 	console.log("localStorage自动登录："+result);
		 	}
		});
	}
	
	//购物车
  	var cart =localStorageService.get("cart");
  	//console.log(angular.toJson(cart));
	var isUndefined=angular.isUndefined(cart);//localStorage中currentUser是否已定义
	if(!isUndefined && cart!=null){	
		Cart.setCart(cart);
	}
	
})

.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider,localStorageServiceProvider) {
	//本地缓存
	localStorageServiceProvider
    .setPrefix('starter')
    .setStorageType('localStorage') //sessionStorage本地关闭后清除
    .setNotify(true, true)
    .setStorageCookie(30, '/');//天数/目录
    
    //不保存视图缓存
    //$ionicConfigProvider.views.maxCache(0);
  // show tab at bottom
	$ionicConfigProvider.platform.android.tabs.position("bottom");
	$ionicConfigProvider.tabs.style('standard');	
  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: "/tab",
    abstract: true,
    templateUrl: "templates/tabs.html",
    controller:'TabsCtrl'
  })

  // Each tab has its own nav history stack:

  .state('tab.index', {
    url: '/index',
    views: {
      'tab-index': {
        templateUrl: 'templates/tab-index.html',
        controller: 'IndexCtrl'
      }
    },
    cache:false
  })

  .state('tab.categories', {
    url: '/categories',
    views: {
      'tab-categories': {
        templateUrl: 'templates/tab-categories.html',
        controller: 'CategoriesCtrl'
      }
    }
  })

  .state('tab.goodstabs', {
      url: '/goodstabs',
      views: {
      'tab-categories': {
      templateUrl: 'templates/goods-tabs.html',
      controller: 'GoodsTabsCtrl'
      }
    }
    })

  .state('tab.goodstabs.goodslist', {
      url: '/goodslist/:csid',
      views: {
      'goodslist': {
        templateUrl: 'templates/goods-list.html',
        controller: 'GoodsListCtrl'
      	}
      }
    })

	.state('goodsinfo', {
      url: '/info/:pid',
      templateUrl: 'templates/goods-info.html',
      controller: 'GoodsInfoCtrl'
    })
	
	.state('goodsdetail', {
		url: '/detail/:pid',
		templateUrl: 'templates/goods-detail.html',
		controller: 'GoodsDetailCtrl'
	})

  	.state('tab.me', {
		url: '/me',
		views: {
		  'tab-me': {
		    templateUrl: 'templates/tab-me.html',
		    controller: 'MeCtrl'
		  }
		},
		cache:false
  })

  .state('myorders', {
    url: '/myorders',
    templateUrl: 'templates/myorders.html',
    controller: 'MyOrdersCtrl',
    cache:false
  })
  
  .state('orderdetail', {
    url: '/orderdetail/:oid',
    templateUrl: 'templates/orderdetail.html',
    controller: 'OrderDetailCtrl'
  })
  
  
  .state('tab.regist', {
    url: '/regist',
    views: {
      'tab-me': {
        templateUrl: 'templates/regist.html',
        controller: 'RegistCtrl'
      }
    }
  })
  
  .state('tab.login', {
    url: '/login',
    views: {
      'tab-me': {
        templateUrl: 'templates/login.html',
        controller: 'LoginCtrl'
      }
    }
  })
  
  .state('tab.settings', {
    url: '/settings',
    views: {
      'tab-me': {
        templateUrl: 'templates/settings.html',
        controller: 'SettingsCtrl'
      }
    }
  })
  
  .state('tab.cart', {
    url: '/cart',
    views: {
      'tab-cart': {
        templateUrl: 'templates/tab-cart.html',
        controller: 'CartCtrl'
      }
    },
    cache:false
  })
  
  .state('order', {
    url: '/order',
    templateUrl: 'templates/order.html',
    controller: 'OrderCtrl'
  })  
  
  .state('orderok', {
    url: '/orderok',
    templateUrl: 'templates/orderok.html',
    params: {oid:null,hehe:null},
    controller: 'OrderOkCtrl'
  })   

  .state('search', {
    url: '/search',
    templateUrl: 'templates/search.html',
    controller: 'SearchCtrl'
  })  

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/index');

});
